package com.kelvin.paopao.module.login.login;

import android.content.Intent;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.hyphenate.EMCallBack;
import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.UserDetailModel;
import com.kelvin.paopao.easemob.EaseMobHelper;
import com.kelvin.paopao.module.login.register.registerStep1.RegisteStep1Activity;
import com.kelvin.paopao.module.main.MainActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.DialogMaker;
import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.paopao.widgets.dialog.OptionSelector;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/9.
 */

public class LoginPresenter implements ILoginPresenter {


    private LoginActivity mView;

    public LoginPresenter(LoginActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }

    @Override
    public void login(final String user_id, String pwd, String weixin_id) {
        RetrofitService.login(user_id, pwd, weixin_id).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView, "登录中,请稍候~~");
            }
        }).subscribe(new Consumer<CommonListResponse<UserDetailModel>>() {
            @Override
            public void accept(@NonNull CommonListResponse<UserDetailModel> userDetailModelCommonListResponse) throws Exception {
                if (userDetailModelCommonListResponse.getReason().equals("成功")) {
                    String user_no = userDetailModelCommonListResponse.getResult().get(0).getUser_no();
                    SpUtil.setLoginHuanxin_id(mView, user_no);

                    String user_nickname = userDetailModelCommonListResponse.getResult().get(0).getUser_name();
                    String user_headimg = userDetailModelCommonListResponse.getResult().get(0).getUser_headimg();

                    SpUtil.setLoginUserName(mView, user_nickname);
                    SpUtil.setLoginAvatar(mView, user_headimg);

                    String user_id = userDetailModelCommonListResponse.getResult().get(0).getUser_tel();
                    SpUtil.setLoginphone(mView, user_id);

                    EaseMobHelper.getInstance().login(user_no, new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            Intent intent = new Intent(mView, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mView.startActivity(intent);
                            DialogMaker.diamiss();
                            Constant.IS_LOGIN = true;
                        }

                        @Override
                        public void onError(int i, String s) {
                            ToastUtil.showToast(mView, "登录失败");
                            DialogMaker.diamiss();
                        }

                        @Override
                        public void onProgress(int i, String s) {

                        }
                    });
                } else {
                    if (userDetailModelCommonListResponse.getReason().equals("尚未绑定过账户")) {
                        mView.showSelector();
                    } else {
                        ToastUtil.showToast(mView, userDetailModelCommonListResponse.getReason());
                    }
                    DialogMaker.diamiss();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable throwable) throws Exception {
                ToastUtil.showToast(mView, "网络错误，请稍后再试~~");
                DialogMaker.diamiss();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
//                DialogMaker.diamiss();
            }
        });
    }
}
